/**
 * org.assess.oa.system.service.impl.TbRoleServiceImpl
 */
package org.assess.oa.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.assess.core.bean.node.TreeNode;
import org.assess.core.common.GeneralUtils;
import org.assess.core.service.BaseServiceImpl;
import org.assess.core.service.ServiceResult;
import org.assess.oa.system.bean.TbRole;
import org.assess.oa.system.bean.TbRoleMenu;
import org.assess.oa.system.bean.TbRoleMenuItem;
import org.assess.oa.system.bean.TbRoleSystem;
import org.assess.oa.system.bean.TbSystem;
import org.assess.oa.system.dao.TbPostRoleDAO;
import org.assess.oa.system.dao.TbRoleDAO;
import org.assess.oa.system.dao.TbRoleMenuDAO;
import org.assess.oa.system.dao.TbRoleMenuItemDAO;
import org.assess.oa.system.dao.TbRoleSystemDAO;
import org.assess.oa.system.dao.TbUserRoleDAO;
import org.assess.oa.system.service.TbRoleService;

import com.alibaba.fastjson.JSON;

/**
 * @description 角色管理的Service层实现类
 * @author XiaLongxiang
 * @createtime 2012-6-8 下午4:46:51
 */
public class TbRoleServiceImpl extends BaseServiceImpl implements TbRoleService {
    /** 操作角色信息的DAO层对象 */
    private TbRoleDAO tbRoleDAO;
    
    /** 操作岗位角色关联信息的DAO层对象 */
    private TbPostRoleDAO tbPostRoleDAO;
    
    /** 操作用户角色关联信息的DAO层对象 */
    private TbUserRoleDAO tbUserRoleDAO;
    
    /** 操作角色系统关联信息的DAO层对象 */
    private TbRoleSystemDAO tbRoleSystemDAO;
    
    /** 操作角色菜单关联信息的DAO层对象 */
    private TbRoleMenuDAO tbRoleMenuDAO;
    
    /** 操作角色菜单项关联信息的DAO层对象 */
    private TbRoleMenuItemDAO tbRoleMenuItemDAO;
    
    private static final Logger logger = Logger.getLogger(TbRoleServiceImpl.class);
    
    public TbRole queryTbRoleById(TbRole role) {
        TbRole result = null;
        try {
            result = this.tbRoleDAO.queryTbRoleById(role.getRole_id());
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public String queryTbRoleAsList(int page, int rows, TbRole role) {
        String result = "";
        try {
            Map<String, Object> parameters = new HashMap<String, Object>();
            
            int totalrecords = this.tbRoleDAO.queryTbRoleAsInt(parameters);
            List<TbRole> records = this.tbRoleDAO.queryTbRoleAsList((page - 1) * rows, rows, parameters);
            result = super.toListPageString(page, rows, totalrecords, "getRole_id", records);
        }
        catch (Exception ex) {
            logger.error(ex);
        }
        return result;
    }
    
    public ServiceResult saveTbRole(TbRole role, boolean edit) throws Exception {
        ServiceResult result = ServiceResult.error_unknown;
        try {
            if (edit) {
                role.setModify_time(new Date());
                role.setModify_user(super.getLoginUserCode());
                this.tbRoleDAO.update(role);
            }
            else {
                role.setRole_id(GeneralUtils.getRandomId(32));
                role.setCreate_time(new Date());
                role.setCreate_user(super.getLoginUserCode());
                this.tbRoleDAO.insert(role);
            }
            
            result = ServiceResult.save_success;
        }
        catch (Exception ex) {
            logger.error(ex);
            throw ex;
        }
        return result;
    }
    
    public String saveRolePermission(TbRole role, TbSystem system, String roleString) throws Exception {
        String result = "";
        try {
            this.tbRoleMenuItemDAO.deleteTbRoleMenuItemByRoleAndSystem(role.getRole_id(), system.getSystem_code());
            this.tbRoleMenuDAO.deleteTbRoleMenuByRoleAndSystem(role.getRole_id(), system.getSystem_code());
            this.tbRoleSystemDAO.deleteTbRoleSystemByRoleAndSystem(role.getRole_id(), system.getSystem_code());
            
            List<TreeNode> nodes = JSON.parseArray(roleString, TreeNode.class);
            if (nodes != null && !nodes.isEmpty()) {
                TbRoleSystem rs = new TbRoleSystem();
                rs.setRole_system_id(GeneralUtils.getRandomId(32));
                rs.setRole_id(role.getRole_id());
                rs.setSystem_code(system.getSystem_code());
                this.tbRoleSystemDAO.insert(rs);
                
                for (TreeNode node : nodes) {
                    if ("menu".equalsIgnoreCase(node.getType())) {
                        TbRoleMenu r = new TbRoleMenu();
                        r.setRole_menu_id(GeneralUtils.getRandomId(32));
                        r.setRole_id(role.getRole_id());
                        r.setMenu_id(node.getId());
                        this.tbRoleMenuDAO.insert(r);
                    }
                    else if ("menuitem".equalsIgnoreCase(node.getType())) {
                        TbRoleMenuItem r = new TbRoleMenuItem();
                        r.setRole_menuitem_id(GeneralUtils.getRandomId(32));
                        r.setRole_id(role.getRole_id());
                        r.setMenuitem_id(node.getId());
                        this.tbRoleMenuItemDAO.insert(r);
                    }
                    else {
                        logger.error("Error TreeNode type when saveRolePermission: " + node.getType());
                    }
                }
            }
            
            result = super.toResultString(true);
        }
        catch (Exception ex) {
            logger.error(ex);
            throw ex;
        }
        return result;
    }
    
    public String deleteTbRole(TbRole role) throws Exception {
        String result = "";
        try {
            String[] role_ids = role.getRole_id().split(",");
            for (String id : role_ids) {
                this.tbRoleMenuItemDAO.deleteTbRoleMenuItemByRoleId(id);
                this.tbRoleMenuDAO.deleteTbRoleMenuByRoleId(id);
                this.tbRoleSystemDAO.deleteTbRoleSystemByRoleId(id);
                this.tbPostRoleDAO.deleteTbPostRoleByRoleId(id);
                this.tbUserRoleDAO.deleteTbUserRoleByRoleId(id);
                this.tbRoleDAO.deleteTbRoleById(id);
            }
            
            result = super.toResultString(true);
        }
        catch (Exception ex) {
            logger.error(ex);
            throw ex;
        }
        return result;
    }
    
    public void setTbRoleDAO(TbRoleDAO tbRoleDAO) {
        this.tbRoleDAO = tbRoleDAO;
    }
    
    public void setTbPostRoleDAO(TbPostRoleDAO tbPostRoleDAO) {
        this.tbPostRoleDAO = tbPostRoleDAO;
    }
    
    public void setTbUserRoleDAO(TbUserRoleDAO tbUserRoleDAO) {
        this.tbUserRoleDAO = tbUserRoleDAO;
    }
    
    public void setTbRoleSystemDAO(TbRoleSystemDAO tbRoleSystemDAO) {
        this.tbRoleSystemDAO = tbRoleSystemDAO;
    }
    
    public void setTbRoleMenuDAO(TbRoleMenuDAO tbRoleMenuDAO) {
        this.tbRoleMenuDAO = tbRoleMenuDAO;
    }
    
    public void setTbRoleMenuItemDAO(TbRoleMenuItemDAO tbRoleMenuItemDAO) {
        this.tbRoleMenuItemDAO = tbRoleMenuItemDAO;
    }
}
